package com.apexsoft.front.common.utils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 类/包过滤工具
 * @author dinglei
 *
 */
public class ClassesUtil {
	
	private static final Logger log = LogManager.getRootLogger();
	
	/**
	 * 根据包路径获取classpath下所有类
	 * @param path 如：com.apexsoft
	 * @return
	 */
	public static Set<Class<?>> getClasses(String path){

		Set<Class<?>> classes = new LinkedHashSet<Class<?>>();
		
		String RESOURCE_PATH = "classpath*:" + StringUtils.replace(path, ".", "/") + "/**/*.class";
		ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
		try {  
			Resource[] resources = resourcePatternResolver.getResources(RESOURCE_PATH);
			MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver); 
			for (Resource e : resources) {  
				MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(e);
				Class cls = ClassUtils.getClass(metadataReader.getClassMetadata().getClassName());
				//System.out.println(cls.getName());
				classes.add(cls);
			}  
		}catch(Exception e){
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return classes;
	}
	
	public static Set<Class<?>> getClassesByAnnotation(String path,Class<? extends Annotation> annotationClass){

		Set<Class<?>> classes = new LinkedHashSet<Class<?>>();

		String RESOURCE_PATH = "classpath*:" + StringUtils.replace(path, ".", "/") + "/**/*.class";
		ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
		try {
			Resource[] resources = resourcePatternResolver.getResources(RESOURCE_PATH);
			MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
			for (Resource e : resources) {
				MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(e);
				Class cls = ClassUtils.getClass(metadataReader.getClassMetadata().getClassName());
				Annotation[] annotations = cls.getAnnotations();
				for(Annotation annotation : annotations){
					if(annotation.annotationType() == annotationClass){
						//System.out.println(cls.getName());
						classes.add(cls);
					}
				}

			}
		}catch(Exception e){
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return classes;
	}

	public static List<String> getEditReqMappingByMethodAnnotation(String path, Class<? extends Annotation> annotationClass){

		List<String> crudUrl = new ArrayList<>();
		String RESOURCE_PATH = "classpath*:" + StringUtils.replace(path, ".", "/") + "/**/*.class";
		ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
		try {
			Resource[] resources = resourcePatternResolver.getResources(RESOURCE_PATH);
			MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
			for (Resource e : resources) {
				MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(e);
				Class cls = ClassUtils.getClass(metadataReader.getClassMetadata().getClassName());
				RequestMapping classReqMapping = (RequestMapping) cls.getAnnotation(RequestMapping.class);
				if (classReqMapping!=null){
					String[] value = classReqMapping.value();
					String classReqMappingUrl = value[0];
					Method[] methods = cls.getMethods();//获取所有方法
					for(Method method:methods){
						if(method.isAnnotationPresent(annotationClass)){
							RequestMapping methodReqMapping = method.getAnnotation(RequestMapping.class);
							if (methodReqMapping!=null){
								String[] methodReqMappingValue = methodReqMapping.value();
								crudUrl.add(classReqMappingUrl+methodReqMappingValue[0]);
							}
						}
					}
				}
			}
		}catch(Exception e){
			log.error(e.getMessage());
			e.printStackTrace();
		}
		return crudUrl;
	}

}
